<?php

//------------------------------------------------------------------
// File: playerDetail.code.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// playerDetail.php codebehind
//------------------------------------------------------------------

define('PROFILE_PIC_PATH', 'images/players/');
define('PROFILE_PIC_DEFAULT', 'default.gif');

require_once('dataAccess.php');
require_once('../lib/utility.php');
require_once('../classes/Player.class.php');
require_once('../classes/Event.class.php');

$playerId = $_GET['player'];

if (is_numeric($playerId))
{
	$player = Player::GetPlayerData($playerId);

	$currentSeason =			CURRENT_SEASON;

	$profileSource =			PROFILE_PIC_PATH . $player->GetProfilePicture();

	$allTimeRating =			$player->GetAllTimeRatingName();
	
	if ($allTimeRating == "AAA")
	{
		$profileSourceRatingBannerImg = "images/player-aaa.png";	
	}
	elseif ($allTimeRating == "AA")
	{
		$profileSourceRatingBannerImg = "images/player-aa.png";	
	}
	elseif ($allTimeRating == "A")
	{
		$profileSourceRatingBannerImg = "images/player-a.png";	
	}
	
	if ($profileSourceRatingBannerImg != "")
	{
		$profileSourceRatingBanner = "<img style=\"position:relative;left:130px;top:130px;\" width=\"70\" height=\"70\" src=\"$profileSourceRatingBannerImg\" />";
	}
	//$profileSourceRatingBanner = $player->GetAllTimeRatingName();
	
	//$profileImage = "<div style=\"background:url(images/players/default.gif); width:200px; height:200px;\"><img style=\"position:relative;left:130px;top:130px;\" width=\"70\" height=\"70\" src=\"images/player-aaa.png\" /></div>";
	$profileImage = "<div style=\"background:url($profileSource); width:200px; height:200px;\">$profileSourceRatingBanner</div>";

	$name =						$player->GetFullName();
	$hometown =					$player->GetHometown();
	$currentTown =				$player->GetCurrentTown();
	$height =					$player->GetHeight();
	$localBeach =				$player->GetLocalBeachName();
	$college = 					$player->GetCollege();
	$currentPlayed =			$player->GetCurrentPlayed();
	$currentPoints =			$player->GetCurrentPoints();
	$currentRating =			$player->GetCurrentRatingName();
	$currentFinish =			$player->GetCurrentBestFinishOrdinal();
	$allTimePlayed =			$player->GetAllTimePlayed();
	$allTimePoints =			$player->GetAllTimePoints();
	//$allTimeRating =			$player->GetAllTimeRatingName();
	$allTimeFinish =			$player->GetAllTimeBestFinishOrdinal();
	$isAVAVolunteer =			$player->GetIsAVAVolunteer();
	$currentOpenWins = 			$player->GetCurrentOpenWins();
	$allTimeOpenWins =			$player->GetAllTimeOpenWins();
	
	if ($currentFinish == '1st')
	{
		if ($currentOpenWins > 1)
			$currentFinish .= " ($currentOpenWins times)";
	}
	
	if ($allTimeFinish == '1st')
	{
		if ($allTimeOpenWins > 1)
			$allTimeFinish .= " ($allTimeOpenWins times)";	
	}
	
	$eventsPlayedCollection =	$player->eventsPlayedCollection;
}
else
{
	// Invalid Player
	// TODO: Provide invalid indicator.
	$profileSource = PROFILE_PIC_PATH . PROFILE_PIC_DEFAULT;
}

function GenerateEventTableRows()
{
	$eventsPlayedCollection = $GLOBALS["eventsPlayedCollection"];
	
	if (count($eventsPlayedCollection) > 0)
	{
		
		
		
		
		
		$i = 0;
		foreach ($eventsPlayedCollection as $key => $value)
		{
			$tournamentId = 	$eventsPlayedCollection[$key]->tournamentId;
			$eventId =			$eventsPlayedCollection[$key]->eventId;
			//$date =			FormatDate($eventsPlayedCollection[$key]->eventDate);
			$date = 			date("F d",strtotime($eventsPlayedCollection[$key]->eventDate));
			$division =			$eventsPlayedCollection[$key]->gender . ' ' . $eventsPlayedCollection[$key]->division;
			$finish =			ConvertIntegerToOrdinal($eventsPlayedCollection[$key]->finish);
			$points =			$eventsPlayedCollection[$key]->points;
			
			if ($points == 0)
				$points = "";
			
			
			$partnerId = 		$eventsPlayedCollection[$key]->partner->GetPlayerId();
			$partnerName = 		$eventsPlayedCollection[$key]->partner->GetFullName();
			
			// Do not display rating, if no rating earned.
			$rating = 			$eventsPlayedCollection[$key]->ratingEarned;
			if ($rating < 4)
				$ratingEarned =	GetRatingName($eventsPlayedCollection[$key]->ratingEarned);
			else
				$ratingEarned = "";
			
			$eventLink = 		"<a href=results2.php?event=$eventId>$date ($division)</a>";
			$partnerLink =		"<a href=playerDetail2.php?player=$partnerId>$partnerName</a>";
			
			
			
			$eventYear =		date("y", strtotime($eventsPlayedCollection[$key]->eventDate));
			
			if ($i == 0)
			{
				$mostRecentSeasonPlayed = date("y", strtotime($eventsPlayedCollection[$key]->eventDate));
				$season = $mostRecentSeasonPlayed;
				$abbreviatedYear = sprintf('%s%s', "'", $season);
				
				echo "<table cellspacing=0>";
				echo "<col  />";
				echo "<col  />";
				echo "<col  />";
				echo "<col  />";
				echo "<thead>";
				echo "<tr class=headings>";
				echo "<th class=year>$abbreviatedYear</th>";
				echo "<th>Partner</th>";
				echo "<th>Finish</th>";
				echo "<th>Earned</th>";
				echo "<th class=last>Points</th>";
				echo "</tr>";
				echo "</thead>";
				echo "<tbody>";
				
				$currentDivision = $division;
			}
			elseif ($eventYear < $season)
			{
				$season = $eventYear;
				
				$abbreviatedYear = sprintf('%s%s', "'", $season);
				echo "<tr class=headings>";
				echo "<th class=year>$abbreviatedYear</th>";
				echo "<th class=year>&nbsp;</th>";
				echo "<th class=year>&nbsp;</th>";
				echo "<th class=year>&nbsp;</th>";
				echo "<th class=year>&nbsp;</th>";
				echo "</tr>";
				
				$currentDivision = $division;
			}
			
			
			if ($currentDivision != $division)
			{
				$currentDivision = $division;
				
				echo "<tr class=alternate-line>";
                echo "<td colspan=5><div class=td-height>&nbsp;</div></td>";
                echo "</tr>";
			}
			
			
			// Alternate rows use css class "alternate"
			//$cssRow =			($i & 1 ? "class=alternate" : "");
			
			// HTML output
			echo "<tr $cssRow>";
			echo "<td>$eventLink</td>";
			echo "<td>$partnerLink</td>";
			echo "<td>$finish</td>";
			echo "<td>$ratingEarned</td>";
			echo "<td class=last>$points</td>";
			echo "</tr>";
			
			$i++;
		}
		
		echo "</tbody>";
        echo "</table>";
		
	}
}
	
?>